package cn.icare.cms.impl;

import cn.icare.cms.ListUtil;
import cn.icare.cms.ShelvesService;
import cn.icare.cms.StringUtil;
import cn.icare.cms.dao.mapper.AreaMapper;
import cn.icare.cms.dao.mapper.ShelvesMapper;
import cn.icare.cms.dao.po.Area;
import cn.icare.cms.dao.po.ItemCat;
import cn.icare.cms.dao.po.Shelves;
import cn.icare.cms.model.ro.AddShelvesRO;
import cn.icare.cms.model.ro.EditShelvesRO;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ShelvesServiceImpl implements ShelvesService {
    @Resource
    private ShelvesMapper shelvesMapper;

    private final Logger logger = LoggerFactory.getLogger(AreaServiceImpl.class);

    @Override
    public Long createOne(AddShelvesRO ro, String actionUserName) {
        Shelves model = new Shelves();
        model.setAreaid(ro.getAreaId());
        model.setCode(ro.getCode());
        model.setName(ro.getName());
        model.setStatus(ro.getStatus());
        model.setCreatetime(new Date());
        model.setCreator(actionUserName);
        Integer result = this.shelvesMapper.insertSelective(model);
        if (result > 0) {
            return model.getId();
        }
        return 0L;
    }

    @Override
    public boolean updateOne(EditShelvesRO ro, String actionUserName) {
        Shelves model = new Shelves();
        model.setId(ro.getId());
        model.setAreaid(ro.getAreaId());
        model.setCode(ro.getCode());
        model.setName(ro.getName());
        model.setStatus(ro.getStatus());
        model.setUpdatetime(new Date());
        model.setUpdator(actionUserName);
        Integer result = this.shelvesMapper.updateByPrimaryKeySelective(model);
        return  result>0;
    }

    @Override
    public boolean deleteOne(Long id) {

      Integer result=  this.shelvesMapper.deleteByPrimaryKey(id);
      return  result>0;
    }

    @Override
    public boolean deleteBatch(List<Long> ids) {
        boolean result = false;
        try {
            if (ListUtil.isNotEmpty(ids)) {
                Example example = new Example(Shelves.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andIn("id", ids);
                result = this.shelvesMapper.deleteByExample(example) > 0;
            }
        } catch (Exception e) {
            this.logger.error("deleteBatch 异常", e);
        }
        return result;
    }

    @Override
    public Shelves getOne(Long id) {
        return this.shelvesMapper.selectByPrimaryKey(id);
    }

    @Override
    public MsgDatasVO getList(Long areaId,String code,  String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        MsgDatasVO result = new MsgDatasVO();
        try {
            Example example = new Example(Shelves.class);
            Example.Criteria criteria = example.createCriteria();
            if (StringUtil.isNotEmpty(code)) {
                criteria.andEqualTo("code",code);
            }
            if (StringUtil.isNotEmpty(name)) {
                criteria.andLike("name", "%" +name + "%");
            }
            if (areaId>0) {
                criteria.andEqualTo("areaid", areaId);
            }
            List<Shelves> dataList = this.shelvesMapper.selectByExample(example);
            PageInfo pageInfo = new PageInfo(dataList);
            result.setTotal(pageInfo.getTotal());
            result.setDatas(dataList);
        } catch (Exception e) {
            this.logger.error("getList 异常", e);
        }
        return result;
    }
}
